package com.obrempon.credit.market.ui.adapter

import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.obrempon.credit.market.R
import com.obrempon.credit.market.utils.ObremponImageUtil
import com.obrempon.module.adapter.ObremponRecyclerViewAdapter
import com.obrempon.module.adapter.ObremponViewHolderHelper
import org.json.JSONObject

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/21 12:02
 * @Annotation：绑卡适配器
 */
class ObremponAuthStepWalletAccountApt(recyclerView: RecyclerView?): ObremponRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        if (model.optBoolean("isEdit",false)){
            return R.layout.obrempon_auth_step_wallet_account_edit_item
        }
        return R.layout.obrempon_auth_step_wallet_account_index_item
    }

    override fun setItemChildListener(helper: ObremponViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.walletEdit) //点击编辑
        helper.setItemChildClickListener(R.id.walletDelete)//删除
    }

    override fun fillData(helper: ObremponViewHolderHelper, position: Int, model: JSONObject) {
        if (position == 0){
            val showText = "*${String.format(mContext.getString(R.string.obrempon_auth_step_wallet_account_index),position+1)}"
            val spString = SpannableString(showText)
            spString.setSpan(ForegroundColorSpan(getColor(R.color.color_ffff4d3e)), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
            helper.setText(R.id.walletIndex,spString)
        } else {
            helper.setText(R.id.walletIndex,String.format(mContext.getString(R.string.obrempon_auth_step_wallet_account_index),position+1))
        }

        //是否是编辑
        val isEdit = model.optBoolean("isEdit",true)
        if (!isEdit){
            helper.setText(R.id.walletTypeTitle,model.optString("walletDesc"))
            helper.setText(R.id.walletAccountMobile,"+" + model.optString("walletMobile"))

            val walletTypeLogo = helper.getImageView(R.id.walletTypeLogo)
            ObremponImageUtil.instance.loadImageUrl(mContext,walletTypeLogo,model.optString("walletLogo"), R.mipmap.obrempon_holder)

            //是否显示 删除按钮
            if (model.has("isShowDelete") && !model.optBoolean("isShowDelete")){
                helper.setVisibility(R.id.walletDelete, View.INVISIBLE)
            } else {
                helper.setVisibility(R.id.walletDelete, View.VISIBLE)
            }
        }
    }
}